<?php
/**
 * Copyright (c) 2016.  hangzhou BOYE .Co.Ltd. All rights reserved
 */

namespace  app\index\controller;


use app\src\admin\controller\BaseController;

use think\Request;

/**
 * Class PublicController
 * @package Admin\Controller
 */

class excel extends BaseController {

    /*
     * excel导出测试
     *
     */

    public function test(){
        $array=[
            ['id','30'],'name','sex',['pas',20]
        ];
        $data=[
            ['1qweqwasdasdaasdasd','1','1','12615616456456145464'],
            ['2','1','1','15415641214514514'],
            ['3','1','1','1'],
        ];
        $name='test';
        $this->outlet($array,$data,$name);
    }


    /*
     * 输出excel表格
     * ￥array为表头
     * data为表数据
     * name为表名
     */
    public function outlet($array,$data,$name) {
        $objPHPExcel = new  \PHPExcel();
        $objProps    = $objPHPExcel->getProperties();

        $count=count($array);

//        for ($i = 1; $i < $count; $i++) {
//            if ($i > 1) {
//                $objPHPExcel->createSheet();
//            }

            $objPHPExcel->setActiveSheetIndex(0);//把当前创建的sheet设置为活动sheet
            $objSheet = $objPHPExcel->getActiveSheet();//获得当前活动Sheet
            $objSheet->setTitle("user" . 1);

        $objSheet->getDefaultStyle()->getFont()->setSize(14);
            $cell_array=['A','B','C','D','E','F','G','H','I','J','K','L','M','N'];
            foreach($array as $k=>$v){
                $cell=$cell_array[$k].'1';
                if(is_array($v)){
                    $objSheet->getColumnDimension($cell_array[$k])->setWidth($v['1']);
                    $objSheet->setCellValue($cell,$v['0']);
                }else{
                    $objSheet->setCellValue($cell,$v);
                }
            }

            $j = 2;
            foreach ($data as $key => $value) {

                foreach($value as $k=>$v){
                    //$objSheet->setCellValue($cell_array[$k]. $j, $v);
                    $objSheet->setCellValueExplicit($cell_array[$k]. $j,$v,\PHPExcel_Cell_DataType::TYPE_NUMERIC);
                }
                $j++;
            }
       // }

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition:attachment;filename='.$name.'.xls');
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');
        exit;
    }


}
